package com.sportschain.app.homepage;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.anychart.charts.Cartesian;
import com.anychart.core.cartesian.series.Area;
import com.anychart.core.ui.Crosshair;
import com.anychart.data.Mapping;
import com.anychart.data.Set;
import com.anychart.enums.HoverMode;
import com.anychart.enums.ScaleStackMode;
import com.anychart.enums.TooltipDisplayMode;
import com.anychart.graphics.vector.Stroke;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sportschain.app.R;
import com.sportschain.app.core.base.BasePagerFragment;
import com.sportschain.app.homepage.activity.StepCountActivity;
import com.sportschain.app.homepage.widget.CircleProgressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomepageFragment extends BasePagerFragment {

    @BindView(R.id.step_pv)
    CircleProgressView mStepPv;
    @BindView(R.id.step_total_tv)
    TextView mStepTotalTv;
    @BindView(R.id.exchange_tv)
    TextView mExchangeTv;
    @BindView(R.id.week_switch)
    Switch mWeekSwitch;
    @BindView(R.id.recommend_rv)
    RecyclerView mRecommendRv;
    @BindView(R.id.step_ll)
    LinearLayout mStepLl;
    @BindView(R.id.step_chart_view)
    AnyChartView mStepChartView;

    private RecommendAdapter mRecommendAdapter;

    public static HomepageFragment newInstance() {

        Bundle args = new Bundle();

        HomepageFragment fragment = new HomepageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        initChartView();
        mRecommendAdapter = new RecommendAdapter();
        mRecommendAdapter.bindToRecyclerView(mRecommendRv);
    }

    @Override
    protected void initData() {
        List data = new ArrayList();
        for (int i = 0; i < 5; i++) {
            data.add(new Object());
        }
        mRecommendAdapter.setNewData(data);
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.step_total_tv, R.id.exchange_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.step_total_tv:
                break;
            case R.id.exchange_tv:
                startActivity(StepCountActivity.class);
                break;
            default:
                break;
        }
    }


    private void initChartView() {
        Cartesian areaChart = AnyChart.area();

        areaChart.animation(true);

        Crosshair crosshair = areaChart.crosshair();
        crosshair.enabled(true);
        // TODO yStroke xStroke in crosshair
        crosshair.yStroke((Stroke) null, null, null, (String) null, (String) null)
                .xStroke("#fff", 1d, null, (String) null, (String) null)
                .zIndex(39d);
        crosshair.yLabel(0).enabled(true);

        areaChart.yScale().stackMode(ScaleStackMode.VALUE);

        areaChart.title(false);

        List<DataEntry> seriesData = new ArrayList<>();
        seriesData.add(new ValueDataEntry("一", 2200));
        seriesData.add(new ValueDataEntry("二", 2800));
        seriesData.add(new ValueDataEntry("三", 2400));
        seriesData.add(new ValueDataEntry("四", 3200));
        seriesData.add(new ValueDataEntry("五", 3400));
        seriesData.add(new ValueDataEntry("六", 3000));
        seriesData.add(new ValueDataEntry("日", 2600));

        Set set = Set.instantiate();
        set.data(seriesData);
        Mapping series1Data = set.mapAs("{ x: 'x', value: 'value' }");

        Area series1 = areaChart.area(series1Data);
        series1.stroke("3 #fff");


        areaChart.legend().enabled(false);
        areaChart.legend().fontSize(13d);
        areaChart.legend().padding(0d, 0d, 20d, 0d);

        areaChart.xAxis(0).title(false);
        areaChart.yAxis(false).title(false);

        areaChart.interactivity().hoverMode(HoverMode.BY_X);
        areaChart.tooltip()
                .displayMode(TooltipDisplayMode.UNION);

        mStepChartView.setChart(areaChart);
    }


    private static class RecommendAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
        public RecommendAdapter() {
            super(R.layout.item_homepage_recommend);
        }

        @Override
        protected void convert(BaseViewHolder helper, Object item) {

        }
    }

}
